/**
 * 最近更新: 2018-10-20
 * 作者: xuek
 * 功能: 前端表单验证
 */

const isempty = val => {
  if (
    val === undefined ||
    val === 'undefined' ||
    val == null ||
    val == 'null' ||
    val == ''
  ) {
    return true
  } else {
    return false
  }
}

// 不为空验证
export const emptyRules = [
  {
    required: true,
    validator: (rule, value, callback) => {
      if (isempty(value)) {
        let field = rule.field
        switch (field) {
          case 'name':
            field = '姓名'
            break
          case 'mobile':
            field = '手机号'
            break
          case 'accountNo':
            field = '银行卡'
            break
          case 'idCard':
            field = '身份证'
            break
          case 'company':
            field = '公司名称'
            break
          case 'account':
            field = '账号'
            break
          case 'area':
            field = '城市'
            break
          case 'address':
            field = '地址'
            break
          case 'position':
            field = '岗位名称'
            break
          case 'invoiceName':
            field = '发票抬头'
            break
          case 'invoiceNumber':
            field = '登记证号'
            break
          case 'contextPerson':
            field = '联系人'
            break
          case 'email':
            field = '邮箱'
            break
          case 'identifyingCode':
          case 'checkCode':
            field = '验证码'
            break
          case 'password':
          case 'oldPassword':
          case 'newPassword':
          case 'passwordConfirm':
          case 'newPasswordConfirm':
            field = '密码'
            break
        }
        callback(new Error(field + '不能为空'))
      } else {
        callback()
        // ant-row ant-form-item ant-form-item-no-colon
      }
    }
  }
]

// 手机号验证
export const mobileRules = [
  {
    required: true,
    validator: (rule, value, callback) => {
      if (value === undefined) {
        callback(new Error('手机号不能为空'))
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('无效的手机号码'))
      } else {
        callback()
      }
    }
  }
]

// 最大值
export const maxAndEmptyRules = num => {
  return [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (value === undefined) {
          callback(new Error('不能为空'))
        } else if (parseFloat(value) > parseFloat(num)) {
          callback(new Error('不能大于' + num))
        } else if (parseFloat(value) < 0) {
          callback(new Error('不能小于0'))
        } else {
          callback()
        }
      }
    }
  ]
}

let cityId = [
  11,
  12,
  13,
  14,
  15,
  21,
  22,
  23,
  31,
  32,
  33,
  34,
  35,
  36,
  37,
  41,
  42,
  43,
  44,
  45,
  46,
  50,
  51,
  52,
  53,
  54,
  61,
  62,
  63,
  64,
  65,
  71,
  81,
  82,
  91
]
function isCardId(idCard, cityId) {
  let [a, b] = [
    cityId.includes(Number(idCard.substr(0, 2))),
    /^\d{6}[12][890]\d{2}[01]\d{1}[0123]\d{4}(\d|x)$/i.test(idCard)
  ]
  if (a && b) {
    return true
  } else {
    return false
  }
}

// 身份证验证
export const idCardRules = [
  {
    required: true,
    validator: (rule, value, callback) => {
      if (value === undefined) {
        callback(new Error('身份证不能为空'))
      } else if (!isCardId(value, cityId)) {
        callback(new Error('无效的身份证号码'))
      } else {
        callback()
      }
    }
  }
]

// 验证 error 信息转换
export const errFormConvert = (errInfo, viewErr) => {
  Object.keys(errInfo).forEach(function(key) {
    viewErr[key] = {
      status: 'error',
      message:
        errInfo[key].errors[0].message || errInfo[key].errors[0].description
    }
  })
  return viewErr
}
